package JDK常用类库;

//import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;

/**
 * @Author：russ
 * @Date：2023/9/13
 * @apiNote：
 * Java的BigDecimal类是用于处理高精度的十进制数的类。
 * 在Java中，double和float类型都有精度限制，对于一些精度要求高的计算场景，可能会出现精度丢失或计算错误的问题。
 */
//@Slf4j
public class BigDecimalTest {

    @Test
    public void test1(){
        float z = 1;
        float h = 0.9f;
        System.out.println(z - h);//0.100000024
        Double a = new Double(0.12);
        Double b = new Double(0.21);
        System.out.println(a + b);//0.32999999999999996
        System.out.println(a - b);//-0.09
        System.out.println(a * b);//0.025199999999999997
        System.out.println(a / b);//0.5714285714285714
    }

    @Test
    public void test2(){
        Double a = new Double(1222222222222222222222222222222.22222);
        System.out.println(a);//1.2222222222222222E30
    }

    @Test
    public void test3(){
        BigDecimal a = new BigDecimal("0.12");
        BigDecimal b = new BigDecimal(0.12);
        System.out.println(a);//0.12
        System.out.println(b);//0.11999999999999999555910790149937383830547332763671875
    }

    @Test
    public void test4(){
        BigDecimal a = new BigDecimal("0.12");
        BigDecimal b = new BigDecimal("0.21");
        System.out.println(a.add(b));//0.33
        System.out.println(a.subtract(b));//-0.09
        System.out.println(a.multiply(b));//0.0252
        System.out.println(a.divide(b,2,BigDecimal.ROUND_HALF_UP));//0.57
    }

    @Test
    public void test5(){
        BigDecimal a = new BigDecimal("0.23");
        BigDecimal b = new BigDecimal("0.23");
        BigDecimal c = new BigDecimal("0.230");
        System.out.println(a == b);//false
        System.out.println(a.equals(b));//true
        System.out.println(a.equals(c));//false
        System.out.println(a.compareTo(b));//0
        System.out.println(a.compareTo(c));//0
    }

    @Test
    public void test6(){
        NumberFormat currency = NumberFormat.getCurrencyInstance(); //建立货币格式化引用
        NumberFormat percent = NumberFormat.getPercentInstance();  //建立百分比格式化引用
        percent.setMaximumFractionDigits(3); //百分比小数点最多3位

        BigDecimal loanAmount = new BigDecimal("15000.48"); //金额
        BigDecimal interestRate = new BigDecimal("0.008"); //利率
        BigDecimal interest = loanAmount.multiply(interestRate); //相乘

        System.out.println("金额:\t" + currency.format(loanAmount));//金额:	￥15,000.48
        System.out.println("利率:\t" + percent.format(interestRate));//利率:	0.8%
        System.out.println("利息:\t" + currency.format(interest));//利息:    ￥120.00
    }

    @Test
    public void test7(){
        BigDecimal a = new BigDecimal("1.2310");
        BigDecimal b = a.setScale(2, RoundingMode.HALF_UP);
        System.out.println(a);//1.2310
        System.out.println(b);//1.23

        BigDecimal c = new BigDecimal("1.2310");
        c = c.setScale(2, RoundingMode.HALF_UP);
        System.out.println(c);//1.23
    }

    @Test
    public void test8(){
        BigDecimal a = new BigDecimal("10.2300");
        a = a.stripTrailingZeros();
        System.out.println(a);//10.23
    }


}
